<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="LAY-list-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">SAP销售单</label>
                    <div class="layui-input-block">
                        <input type="text" name="order_sn_sap" placeholder="" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">店铺</label>
                    <div class="layui-input-block">
                        <select name="shop" lay-search>
                            <option value="">请选择店铺</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">SKU</label>
                    <div class="layui-input-block">
                        <input type="text" name="sku" placeholder="" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">发货时间</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input" id="test-laydate-type-date" name="ship_time" placeholder="yyyy-MM-dd">
                    </div>
                </div>
                <div class="layui-inline">
                    <button style="display:none" class="down" lay-submit="" lay-filter="LAY-list-down">下载</button>
                    <input type="hidden" class="layui-input" id='type' name="type" value='normal'>
                    <button type="reset" class="layui-btn layui-btn-primary reset" id="reset">重置</button>
                    <button class="layui-btn layuiadmin-btn-useradmin search" lay-submit lay-filter="LAY-list-search2">搜索</button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">

            <div class="layui-card-body">
                <div class="layui-tab" lay-filter="showSapInfo" id='showSapInfo'>
                    <ul class="layui-tab-title">
                        <li class="layui-this" lay-id="normal" data-id='normal'>已出库信息</li>
                        <li lay-id="absolute" data-id='absolute'>异常信息</li>
                    </ul>
                    <div class="layui-tab-content">
                        <div class="layui-tab-item layui-show">
                            <table id="LAY-list-normal"  lay-filter="LAY-list-normal"></table>
                        </div>
                        <div class="layui-tab-item"><table id="LAY-list-absolute"  lay-filter="LAY-list-absolute"></table></div>
                    </div>
                </div>
            </div>
            <!-- 头部工具栏 -->
            <script type="text/html" id="toolbar">
                <div class="layui-btn-group fl layui-btn-group-jy toolbargn">
                    <a lay-event="export" href="javascript:;" class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-download-circle">&nbsp;导出</a>
                </div>
                </script>
            </div>
        </div>
    </div>
    <script>
        layui.use(['admin', 'table', 'form', 'toolJ', 'upload', 'laydate', 'element'], function () {
            var $ = layui.$
                    , admin = layui.admin
                    , view = layui.view
                    , table = layui.table
                    , toolJ = layui.toolJ
                    , form = layui.form
                    , upload = layui.upload
                    , laydate = layui.laydate
                    , element = layui.element;
            //国家sku管理
            var shop = layui.data('yujiso').shop;
            var selectHtml = '';
            for (var i in shop) {
                selectHtml += '<option value="' + i + '">' + shop[i] + '</option>';
            }
            $("select[name='shop']").append(selectHtml);
            //日期选择器
            laydate.render({
                elem: '#test-laydate-type-date'
                        //,type: 'date' //type 默认为 date，所以可不填
            });
            form.render(null, 'LAY-list-form');
            viewTable();
            element.on('tab(showSapInfo)', function () {
                var layid = $(this).attr('lay-id');
                $("#type").val(layid);
               $('.search').click();
                
            });
            var i=0;
            //监听搜索
            layui.form.on('submit(LAY-list-search2)', function (data) {
                var field = data.field;
                layid = $("#type").val();
                if(i!=1 && layid!=''){
                    viewTable(layid); 
                }
                if (layid == 'absolute') {
                     str = '<a lay-event="batchUpdate" href="javascript:;" class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-download-circle">&nbsp;更新异常</a>';
                    //执行重载
                    layui.table.reload('LAY-list-absolute', {
                        where: field,
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    layui.table.render(null, 'LAY-list-absolute');
                } else {
                    str = '<a lay-event="export" href="javascript:;" class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-download-circle">&nbsp;导出</a>';
                    //执行重载
                    layui.table.reload('LAY-list-normal', {
                        where: field,
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    //   $(".search").click();
                    layui.table.render(null, 'LAY-list-normal');
                }
                $('.toolbargn').html(str);
            });
            function  viewTable(layid = '') {
                var param = [
                    {field: 'shop_name', title: '店铺'}
                    , {field: 'order_sn_sap', title: 'SAP销售单号'}
                    , {field: 'order_time', title: '订单时间'}
                    , {field: 'seller', title: '销售人员'}
                    , {field: 'sku', title: 'SKU'}
                    , {field: 'order_quantity', title: '销售数量'}
                    , {field: 'order_price', title: '销售单价'}
                    , {field: 'money', title: '销售金额'}
                    , {field: 'currency', title: '币种'}
                    , {field: 'rmbamount', title: '人民币金额'}
                    , {field: 'exchange_rate', title: '汇率'}
                    , {field: 'ship_warehouse', title: '发货仓'}
                    , {field: 'ship_time', title: '发货时间'}
                ];

                if (layid == '' || layid == 'normal') {
                    i=1;
                    layid = 'normal';
                } else {
                    i=1;
                    layid = 'absolute';
                    param[13] = {field: 'abnormalResult', title: '异常备注'};
                }
                var ele = 'LAY-list-' + layid;
                table.render({
                    elem: "#" + ele
                    , url: '/index/Storesales/getList'
                    , where: {type: layid}
                    , toolbar: '#toolbar'
                    , defaultToolbar: ['filter']
                    , cols: [param]
                    , page: true
                    , limit: 10
                    ,text:{none: '暂无相关数据!' }
                });
                //监听头部工具栏 操作
                table.on('toolbar(' + ele + ')', function (obj) {
                    //event对应模板的
                    switch (obj.event) {
                        case 'export':
                            $('.down').click();
                            break;
                        case 'batchUpdate':
                            toolJ.ajax({"url": "/index/Storesales/batchUpdateAbsolute"});
                            break;
                    }
                });
            }
        });
    </script>
